package hu.example.munka_count.model;

import java.util.ArrayList;
import java.util.List;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;

public class DateDB {
	private SQLiteDatabase database;
	private DataBase db;
	private String[] allColumns = { DataBase.colID, DataBase.colCurrDate,
			DataBase.colStartMills, DataBase.colEndMills, };

	public DateDB(Context context) {
		db = new DataBase(context);
	}

	public void open() throws SQLException {
		database = db.getWritableDatabase();
	}

	public void close() {
		db.close();
	}
	
	public String getPath(){
		return database.getPath();
	}
	
	public DateModel createDate(String cd, String sm) {
		ContentValues record = new ContentValues();
		record.put(DataBase.colCurrDate, cd);
		record.put(DataBase.colStartMills, sm);

		long insertId = database.insert(DataBase.dbName, null, record);

		Cursor cursor = database.query(DataBase.dbName, allColumns,
				DataBase.colID + " = " + insertId, null, null, null, null);

		cursor.moveToFirst();
		DateModel newDate = cursorToDate(cursor);
		cursor.close();
		return newDate;
	}

	/*
	 * public void deleteComment(Comment comment) { long id = comment.getId();
	 * System.out.println("Comment deleted with id: " + id);
	 * database.delete(DataBase.TABLE_COMMENTS, DataBase.COLUMN_ID + " = " + id,
	 * null); }
	 */
	public List<DateModel> getAllDates() {
		List<DateModel> models = new ArrayList<DateModel>();

		Cursor cursor = database.query(DataBase.dbName, allColumns, null, null,
				DataBase.colStartMills, null, null);

		cursor.moveToFirst();
		while (!cursor.isAfterLast()) {
			DateModel model = cursorToDate(cursor);
			models.add(model);
			cursor.moveToNext();
		}
		// Make sure to close the cursor
		cursor.close();
		return models;
	}

	public DateModel getById(int id) {
		Cursor cursor = database.query(DataBase.dbName, allColumns, "id = "
				+ id, null, null, null, null);

		cursor.moveToFirst();
		DateModel model = cursorToDate(cursor);
		cursor.close();
		return model;
	}

	public DateModel getLastRecord() {
		Cursor cursor = database.query(DataBase.dbName, allColumns,
				"id = (SELECT MAX(id) FROM " + DataBase.dbName + ")", null,
				null, null, null);
		
		DateModel model = null;
		if (cursor.getCount() != 0) {
			cursor.moveToFirst();
			model = cursorToDate(cursor);
		}
		cursor.close();
		return model;
	}

	public void removeAll(){
		database.delete(DataBase.dbName, null, null);
	}
	
	public void updateByObj(DateModel obj){
		ContentValues record = new ContentValues();
		record.put(DataBase.colEndMills, obj.getEndMills());
		
		database.update(DataBase.dbName, record, "id = "+obj.getId(), null);
	}
	
	private DateModel cursorToDate(Cursor cursor) {
		DateModel model = new DateModel();
		model.setId(cursor.getInt(0));
		model.setCurrDate(cursor.getString(1));
		model.setStartMills(cursor.getString(2));
		model.setEndMills(cursor.getString(3));
		return model;
	}
	
	
}
